package com.ymatou.iapi.livebuyer.testcase;

import com.ymatou.iapi.livebuyer.parameter.SubscribeBean;
import com.ymatou.iapi.livebuyer.service.SubscribeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;
import java.util.Map;
/**
*直播订阅
*FreeMaker Template自动生成代码
 * 直播订阅增加区分来源
 * @2018/09/12
*/
@TestSuite
public class Ts_Subscribe{
	private static SubscribeBean subscribeBean;
	private static SubscribeCall subscribeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播订阅");
	}

	@Before
	public void caseUp() {
		subscribeBean=new SubscribeBean();
		subscribeCall=new SubscribeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(subscribeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_Subscribe_001() {
		Logger.start(true,"直播订阅，成功，来源App(默认为0)");
		try {
			//int liveid=new LiveRoomWapper().selectLastLiveRoom().getId();
            LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			int  userId=4085;
			subscribeBean.setLiveId(createlive.getId());
			subscribeBean.setUserId(userId);
			subscribeBean.setSource(0);
			subscribeCall.setData(subscribeBean);
			subscribeCall.callService();			
			Logger.verifyEquals("200", subscribeCall.getString("code"), "判断code");			
			Map m=new LiveRoomWapper().selectLiveSubscribeById(createlive.getId(),userId).get(0);
			Logger.verifyEquals(1, Integer.parseInt(m.get("Status").toString()),"验证Status");
			Logger.verifyEquals(0, Integer.parseInt(m.get("Source").toString()),"验证Souce");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_Subscribe_002() {
		Logger.start(true,"直播订阅，成功，来源wxapp");
		try {
			//int liveid=new LiveRoomWapper().selectLastLiveRoom().getId();
            int liveid = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null)).getId();
            int  userId=4085;
			subscribeBean.setLiveId(liveid);
			subscribeBean.setUserId(userId);
            subscribeBean.setSource(1);
            String formId = "1x00087cde975f21a700252b3e761b738";
            subscribeBean.setFormId(formId);
			subscribeCall.setData(subscribeBean);
			subscribeCall.callService();
			Logger.verifyEquals("200", subscribeCall.getString("code"), "判断code");
			Map m=new LiveRoomWapper().selectLiveSubscribeById(liveid,userId).get(0);
			Logger.debug(m.toString());
			Logger.verifyEquals(1, Integer.parseInt(m.get("Status").toString()),"验证OnlineNum");
            Logger.verifyEquals(1, (Integer)m.get("Source"),"验证Souce");
            Logger.verifyEquals(formId, m.get("FormId").toString(),"formId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}



    @Test
    @Category({P1.class})
    @TestCase
    public void Ts_Subscribe_003() {
        Logger.start(true,"直播订阅，成功，来源wxapp,formId长度超过指定长度");
        try {
            int liveid = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null)).getId();
            int  userId=4085;
            subscribeBean.setLiveId(liveid);
            subscribeBean.setUserId(userId);
            subscribeBean.setSource(1);
            subscribeBean.setFormId("1x00087cde975f21a700252b3e761b738XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
            subscribeCall.setData(subscribeBean);
            subscribeCall.callService();
            Logger.verifyEquals("100", subscribeCall.getString("code"), "判断code");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_Subscribe_004() {
		Logger.start(true,"直播订阅，成功，来源wxapp,不传formId");
		try {
			int liveid = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null)).getId();
			int  userId=4085;
			subscribeBean.setLiveId(liveid);
			subscribeBean.setUserId(userId);
			subscribeBean.setSource(1);
			subscribeCall.setData(subscribeBean);
			subscribeCall.callService();
			Logger.verifyEquals("100", subscribeCall.getString("code"), "判断code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_Subscribe_005() {
		Logger.start(true,"直播订阅，成功，来源wxapp和app");
		try {
			//int liveid=new LiveRoomWapper().selectLastLiveRoom().getId();
			int liveid = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null)).getId();
			int  userId=4085;
			subscribeBean.setLiveId(liveid);
			subscribeBean.setUserId(userId);
			subscribeBean.setSource(1);
			String formId = "1x00087cde975f21a700252b3e761b738";
			subscribeBean.setFormId(formId);
			subscribeCall.setData(subscribeBean);
			subscribeCall.callService();

			SubscribeBean subscribeBean1 = new SubscribeBean();
			subscribeBean1.setSource(0);
			subscribeBean1.setLiveId(liveid);
			subscribeBean1.setUserId(userId);
			subscribeCall.setData(subscribeBean1);
			subscribeCall.callService();
			Logger.verifyEquals("200", subscribeCall.getString("code"), "判断code");
			List<Map> map = new LiveRoomWapper().selectLiveSubscribeById(liveid,userId);
			Logger.verifyEquals(true,map.size()==2,"验证有两个直播订阅");
			/*Logger.verifyEquals(1, Integer.parseInt(m.get("Status").toString()),"验证Status");
			Logger.verifyEquals(1, (Integer)m.get("Source"),"验证Souce");
			Logger.verifyEquals(formId, m.get("FormId").toString(),"formId");*/
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}